#!/bin/bash
#IModule

#########################
#   名称: Logger
#   用途: 提供一些基础的输出功能
#########################
if [ -z "$_Logger_Loaded" ];then
    function _framework_resolve_dependencies()
    {
        true;
    }

    readonly _Logger_Color_Green="\33[32m";
    readonly _Logger_Color_Yellow="\33[33m";
    readonly _Logger_Color_Red="\33[31m";
    readonly _Logger_Color_White="\33[0m";

    function _Logger_LogFromPipe()
    {
        local LogLevel="$1";
        local Multiline="${2:-"false"}";
        local ExtraMessage="$3";
        local Message="";
        if [ "$Multiline" == true ];then
            while read -r Message;do
                _Logger_Log "$LogLevel" "$Message";
            done;
        else
            read -r Message;
            _Logger_Log "$LogLevel" "${ExtraMessage}${Message}";
        fi;
    }

    function _Logger_Log()
    {
        local LogLevel="${1:-"Info"}";
        local Message="${2}";

        local Color="";
        case "$LogLevel" in
            (Ok)
                Color="${_Logger_Color_Green}";
                LogLevel=" OK ";
                ;;
            (Warn | Warning)
                Color="${_Logger_Color_Yellow}";
                LogLevel="Warn";
                ;;
            (Err | Erro | Error)
                Color="${_Logger_Color_Red}";
                LogLevel="Erro";
                ;;
            (Info)
                Color="${_Logger_Color_White}";
                ;;
            (*)
                Color="${_Logger_Color_White}";
                _Logger_Log Erro "[Logger] 未知的日志等级 \"$LogLevel\"";
                LogLevel="Info";
                Message="$1";
        esac;

        printf "[%s][$(date +%T)]${Color}[${LogLevel}]\t%s\33[0m\n" "${_framework_APPNAME:?}" "$Message" >&2;
    }

    readonly _Logger_Loaded=true;
fi;
